<link rel="stylesheet" href="/assets/home/plugins/mescroll/mescroll.min.css">
<link rel="stylesheet" href="/assets/home/css/order.css">

<header class="mui-bar mui-bar-nav mui-bar-nav-bg">
    <a id="icon-menu" class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
    <h1 class="mui-title">我的提现</h1>
</header>
<br />
<br />
<br />
<div id="money" class="mescroll">
    <div class="list">
    </div>
</div>

<script src="/assets/home/plugins/mescroll/mescroll.min.js"></script>
<script>

    // 实例化插件
    let meScroll = new MeScroll('money', {
        // 下拉刷新
        down: {
            auto: false,//是否在初始化完毕之后自动执行下拉回调callback; 默认true
            callback: downCallback // 下拉刷新的回调,别写成downCallback(),多了括号就自动执行方法了
        },
        // 上拉加载
        up: {
            auto: true, //是否在初始化时以上拉加载的方式自动加载第一页数据; 默认false
            isBounce: false, //此处禁止ios回弹,解析(务必认真阅读,特别是最后一点): http://www.mescroll.com/qa.html#q10
            callback: UpCallback, //上拉回调,此处可简写; 相当于 callback: function (page) { upCallback(page); }
            toTop: { //配置回到顶部按钮
                duration: 600,
                src: "/assets/home/images/mescroll-totop.png", //默认滚动到1000px显示,可配置offset修改
                offset: 100
            }
        }
    });

    async function downCallback() {

        meScroll.resetUpScroll();
        $('#money .list').empty();
        return false;
    }


    async function UpCallback(page) {
        let data = {
            page: page.num,
            limit: page.size,
        }

        let result = await POST({
            url: 'business/money',
            params: data
        });
        console.log(result);

        if (result.code === 1) {
            let moneylist = result.data;
            meScroll.endBySize(moneylist.code.length);
            fetchList(moneylist);
        }
        else {
            mui.toast("提现失败");
            meScroll.endBySize(0, 0);
            return false;
        }
    }


    function fetchList(list) {

        let Item = '';
        let btnTemp = '';
        for (let i = 0; i < list.code.length; i++) {
            if (list.status[i] == 1) {
                btnTemp = `<button class="btn disabled">已提现</button>`;
            } else {
                btnTemp = `<button class="btn mui-btn-primary" data-orderid="${list.order[i]}" data-amount="${list.amount[i]}">提现</button>`
            }
            Item = `
                    <div class="item">
                        <div class="content">
                            <div class="thumb">
                                <img src="#" alt="">
                            </div>
                            <div class="info">
                                <div class="code">
                                    <span>订单号：</span>${list.code[i]}
                                </div>
                                <div class="name">
                                    <span>课程名称：</span>${list.subject[i]}
                                </div>
                                <div class="type">
                                    <span>消费价格：</span>${list.total[i]}
                                </div>
                                <div class="price">
                                    <span>提现：</span>￥${list.amount[i]}
                                </div>
                                <div class="time">
                                    <span>购买时间：</span>${list.time[i]}
                                </div>
                            </div>
                        </div>
                
                        <div class="footer">
                            <div class="btn-list">
                            ${btnTemp}
                            </div>
                        </div>
                    </div>
                `;

            $('#money .list').append(Item);
        }
    }

    $(document).on('click', '.mui-btn-primary', async function (e) {

        let btn = $(event.target);

        let orderid = $(event.target).data('orderid');
        let amount = $(event.target).data('amount');

        let data = {
            orderid,
            amount
        }

        let result = await POST({
            url: 'business/commission',
            params: data
        });

        console.log(result);

        if (result.code === 1) {
            btn.removeClass('mui-btn-primary').addClass('disabled').text("已提现");
            mui.toast("提现成功");
        }
    })

    $(document).on('click', '.disabled', function (e) {
        mui.toast("已提现完成");
    })
</script>